package coint.btc.config;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
import org.springframework.web.bind.annotation.RequestMethod;


public class JWTFilter extends BasicHttpAuthenticationFilter {
    private static String Token;
//	/**
//	 * 对请求进行跨域支持
//	 */
//	@Override
//	protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
//		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
//		httpServletResponse.setHeader("Access-control-Allow-Origin", httpServletRequest.getHeader("Origin"));
//		httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
//		httpServletResponse.setHeader("Access-Control-Allow-Headers",
//				httpServletRequest.getHeader("Access-Control-Request-Headers"));
//		// 跨域时会首先发送一个option请求，这里我们给option请求直接返回正常状态
//		if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
//			httpServletResponse.setStatus(HttpStatus.OK.value());
//			return false;
//		}
//		return super.preHandle(request, response);
//	}
//
//	@Override
//	protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
//		System.out.println(isLoginAttempt(request, response) + "--JWTFilter.class");
//		if(isLoginAttempt(request, response)) {
//			try {
//				executeLogin(request, response);
//			} catch (Exception e) {
//				e.printStackTrace();
//				logout(request, response);
//			}
//		}else {
//			logout(request, response);
//		}
//		return true;
//	}
//
//	private void logout(ServletRequest request, ServletResponse response) {
//		HttpServletRequest req = (HttpServletRequest) request;
//		req.setAttribute("access_token", "Hyuhjk90QWEPOUnvmchsbx67QEGF=");
//	}
//
//	@Override
//	protected boolean isLoginAttempt(ServletRequest request, ServletResponse response) {
//		HttpServletRequest req = (HttpServletRequest) request;
//        String authorization = req.getHeader("access_token");
//      
//        DaozhaAdmin admin = new DaozhaAdmin();
//        admin.setName(PropetiesUtils.getContentByKey("Login.UserName"));
//        admin.setPassword(PropetiesUtils.getContentByKey("Login.PassWord"));
//        admin.setJueseId(6);
//        admin.setNickname("李涛");
//        admin.setPhone("15200812940");
//        admin.setRemark("拥有最高权限");
//        admin.setState(1);
//        admin.setUserId(3);
//		if(StringUtils.isBlank(authorization)) {
//			authorization=JWTUtil.sign(admin);
//			Token=authorization;
//		}
//		
//		 System.out.println("-------access_token-------"+authorization);
//        if(StringUtils.isNotBlank(authorization)) {
//        	return JWTUtil.verify(authorization);
//        }
//        return false;
//	}
//
//	@Override
//	protected boolean executeLogin(ServletRequest arg0, ServletResponse arg1) throws Exception {
//		HttpServletRequest httpServletRequest = (HttpServletRequest) arg0;
//       // String authorization = httpServletRequest.getHeader("access_token");
//		String authorization=Token;
//        DaozhaAdmin user = JWTUtil.getUser(authorization);
//        UsernamePasswordToken token = new UsernamePasswordToken(user.getName(), user.getPassword());
//		Subject subject = SecurityUtils.getSubject();
//		subject.login(token);
//        return true;
//	}
}
